package com.bytech.article.biz.controller.tag;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytech.article.biz.service.articleTag.ArticleTagService;
import com.bytech.common.biz.entity.article.ArticleDto;
import com.bytech.common.biz.entity.articleTag.ArticleTag;
import com.bytech.common.biz.entity.articleTag.ArticleTagDto;
import com.bytech.common.biz.entity.articleTag.ArticleTagQuery;
import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.enums.CommonStatusEnum;

/**
 * @Author Zhao
 * @Create 2021/8/13 15:54
 */
@RestController
@RequestMapping("/articleTag")
public class ArticleTagController {

    @Resource
    private ArticleTagService articleTagService;

    @GetMapping("/getAllUsable")
    public List<ArticleTag> getAllUsableTag() {
        LambdaQueryWrapper<ArticleTag> tagQuery = new LambdaQueryWrapper<>();
        tagQuery.eq(ArticleTag::getTagStatus, CommonStatusEnum.NORMAL.getValue());
        return articleTagService.list(tagQuery);
    }

    @GetMapping("/getAllUsableWithCount")
    public BaseResult<List<ArticleTagDto>> getAllUsableTagWithArticleCount() {
        return articleTagService.getAllUsableTagWithArticleCount();
    }

    @PostMapping("/pageTagByQuery")
    public BaseResult<Page<ArticleTag>> pageArticleByQuery(@RequestBody ArticleTagQuery<ArticleTag> tagQuery) {
        return BaseResult.<Page<ArticleTag>>success().data(articleTagService.pageTagByQuery(tagQuery)).build();
    }

    @PostMapping("/save/")
    public BaseResult<ArticleTag> doSaveArticleTag(@RequestBody ArticleTagDto tagDto) {
        return articleTagService.saveTag(tagDto);
    }

    @GetMapping("/getTagById")
    public BaseResult<ArticleTag> getTagById(@RequestParam String id) {
        return BaseResult.<ArticleTag>success().data(articleTagService.getById(id)).build();
    }

    /**
     * 逻辑删除
     */
    @PostMapping("/delete")
    public BaseResult<String> deleteTagById(@RequestParam String id) {
        ArticleTag tag = articleTagService.getById(id);
        if (tag == null) {
            return BaseResult.<String>error("标签不存在！").data(null).build();
        }
        tag.setTagStatus(CommonStatusEnum.DELETED.getValue());
        articleTagService.updateById(tag);
        return BaseResult.<String>success("删除成功！").data(null).build();
    }

}
